找传奇、传世资源到传世资源站!
Google 正文

GoogleChrome与js交互实例(Chromium)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

GoogleChrome与js交互实例(Chromium) Google-第1张 using CefSharp.WinForms;using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace GoogleChromeTest{ public partial class Form1 : Form { /// <summary> /// Chromium浏览器实例 /// </summary> ChromiumWebBrowser WebBrowser; public Form1() { InitializeComponent(); ///设置 var setting = new CefSharp.CefSettings(); setting.Locale = "zh-CN"; setting.CachePath = "CHBrowser/BrowserCache";//缓存路径 setting.AcceptLanguageList = "zh-CN,zh;q=0.8";//浏览器引擎的语言 setting.LocalesDirPath = "CHBrowser/localeDir";//日志 setting.LogFile = "CHBrowser/LogData";//日志文件 setting.PersistSessionCookies = true;// setting.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";//浏览器内核 setting.UserDataPath = "CHBrowser/userData";//个人数据 ///初始化 CefSharp.Cef.Initialize(setting); WebBrowser = new ChromiumWebBrowser("http://www.baidu.com"); //初始页面 WebBrowser.RegisterJsObject("jsObj", new JsEvent(), new CefSharp.BindingOptions() { CamelCaseJavascriptNames = false }); //交互数据 WebBrowser.Dock = DockStyle.Fill;//铺满 WebBrowser.Dock = DockStyle.Fill;//设置停靠方式 this.Controls.Add(WebBrowser);//加入窗体 } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { WebBrowser.Load(textBox1.Text);//浏览网址 } private async void button2_Click(object sender, EventArgs e) { await WebBrowser.GetBrowser().MainFrame.EvaluateScriptAsync("test_val=" new Random().Next().ToString("F")); //设置页面上js的test_val变量为随机数 await WebBrowser.GetBrowser().MainFrame.EvaluateScriptAsync("test()");//运行页面上js的test方法 } private async void button3_Click(object sender, EventArgs e) { await WebBrowser.GetBrowser().MainFrame.EvaluateScriptAsync("testArg('123','我是NET' )");//运行页面上js的testArg带参数的方法 } } public class JsEvent { public string MessageText { get; set;} public void ShowTest() { MessageBox.Show("this in C#.\n\r" MessageText); } public void ShowTestArg(string ss) { MessageBox.Show("收到JS带参数调用\n\r" ss); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复